<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" th:href="@{/xadmin/css/font.css}">
    <link rel="stylesheet" th:href="@{/xadmin/css/xadmin.css}">
    <script th:src="@{/xadmin/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/xadmin/js/xadmin.js}"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        .layui-table-body {
            overflow-x: hidden;
        }
    </style>
</head>

<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">用户管理</a>
                <a>
                    <cite>用户列表</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <div class="layui-card-body hide_form" style="display: none;">
                        <form class="layui-form layui-col-space5">
                            <div class="layui-inline layui-show-xs-block">
                                <input class="layui-input" required autocomplete="off" placeholder="输入用户名或姓名"
                                       name="value"
                                       lay-verify="required" lay-reqtext="不能为空">
                            </div>
                            <div class="layui-inline">
                                <input type="radio" name="type" value="realName" title="根据姓名查询" checked>
                                <input type="radio" name="type" value="username" title="根据用户名查询">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn" lay-submit="" lay-filter="search">
                                    <i class="layui-icon">查询</i></button>
                            </div>
                        </form>
                    </div>
                    <div class="layui-card-body">
                        <table class="layui-table" id="test" lay-filter="test"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <div class="layui-inline" title="批量删除" lay-event="delete"><i class="layui-icon layui-icon-delete"></i></div>
        <div class="layui-inline" title="添加" lay-event="add"><i class="layui-icon layui-icon-add-1"></i></div>
        <div class="layui-inline" title="搜索" lay-event="search"><i class="layui-icon layui-icon-search"></i></div>
    </div>
</script>
<script>
    layui.use('table', function () {
        var table = layui.table;

        table.render({
            elem: '#test',
            url: '/user',
            toolbar: '#toolbarDemo',
            cols: [
                [
                    {type: 'checkbox', title: 'ID'},
                    {type: 'numbers', width: 80, sort: true, title: '序号'},
                    {field: 'username', minWidth: 100, sort: true, title: '用户名'},
                    {field: 'realName', minWidth: 60, title: '姓名'},
                    {field: 'email', minWidth: 120, title: '邮箱'},
                    {field: 'roleName', minWidth: 80, title: '角色'},
                    {toolbar: '#barOptional', minWidth: 100, title: '操作'}
                ]
            ],
            page: true,
            limits: [10, 20, 50, 100, 500, 1000]
        });

        //头工具栏事件
        table.on('toolbar(test)',
            function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'delete':
                        var data = checkStatus.data;
                        var idArray = [];
                        for (var i in data) {
                            idArray[i] = data[i].id;
                        }
                        if (idArray.length === 0) {
                            layer.alert("未选中", {icon: 5});
                            break;
                        }
                        console.log(idArray.toString());
                        layer.confirm('真的要删除用户? 请谨慎操作!', function (index) {
                            var load = layer.load(2)
                            $.ajax({
                                url: '/user/users',
                                type: "DELETE",
                                data: {ids: idArray},
                                dataType: 'json',
                                success: function (result) {
                                    layer.close(load)
                                    if (result.code === 500 || result.code === 5000100 || result.code === 5000101 || result.code === 5000102) {
                                        layer.alert(result.msg, {icon: 2});
                                    } else {
                                        layer.alert("删除成功", {icon: 1}, function (index1) {
                                            layer.close(index1);
                                            //xadmin.father_reload();
                                            table.reload('test');
                                        });
                                    }
                                },
                            });
                        });
                        // layer.alert(JSON.stringify(data));
                        break;
                    case 'add':
                        xadmin.open('用户添加', '/admin/getPage?name=user/user-add');
                        break;
                    case 'search':
                        let hide_add = $('.hide_form');
                        if (hide_add.css('display') === "none") {
                            hide_add.show();
                        } else {
                            hide_add.hide();
                        }
                        break;
                }
            });
    });


    //监听工具条
    layui.use(['table', 'form'], function () {
        var table = layui.table
            , form = layui.form;
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            var roleStr = data.role.replace("ROLE_", "").toLocaleLowerCase();
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    var load = layer.load(2)
                    $.ajax({
                        url: '/user/' + roleStr,
                        type: "DELETE",
                        data: {id: data.id},
                        dataType: 'json',
                        success: function (result) {
                            layer.close(load)
                            if (result.code === 500 || result.code === 5000100 || result.code === 5000101 || result.code === 5000102) {
                                layer.alert(result.msg, {icon: 2});
                            } else {
                                layer.alert("删除成功", {icon: 1}, function (index1) {
                                    layer.close(index1);
                                    //xadmin.father_reload();
                                    table.reload('test');
                                });
                            }
                        },
                    });

                });
            } else if (obj.event === 'edit') {
                // console.log($(window).width())
                xadmin.open('用户详情', '/user/' + roleStr + '/edit/?id=' + data.id);
            } else if (obj.event === 'resetPassword') {
                layer.confirm('确定恢复密码？', function (index) {
                    var load = layer.load(2)
                    $.ajax({
                        url: "/user/resetPassword",
                        type: "POST",
                        data: {userId: data.id},
                        dataType: 'json',
                        success: function (result) {
                            layer.close(load)
                            console.log("success")
                            layer.alert("恢复成功", {icon: 1}, function (index1) {
                                layer.close(index1);
                                //xadmin.father_reload();
                                table.reload('test');
                            });
                        },
                        error: function (result) {
                            console.log("fail")
                            console.log(result)
                            layer.alert(result.msg, {icon: 2}, function (index1) {
                                layer.close(index1);
                                //xadmin.father_reload();
                                table.reload('test');
                            });
                        }
                    });
                });
            }
        })

        form.on('submit(search)', function (data) {
            console.log(data.field);
            sessionStorage.setItem('search_value', data.field['value']);
            sessionStorage.setItem('search_type', data.field['type']);
            var load = layer.load(2)
            $.ajax({
                url: "/user/search?page=1&limit=10",
                type: "GET",
                data: {
                    value: data.field['value'],
                    type: data.field['type']
                },
                dataType: 'json',
                success: function (result) {
                    layer.close(load)
                    if (result.code === 500 || result.code === 5000100 || result.code === 5000101 || result.code === 5000102) {
                        layer.alert(result.msg, {icon: 2});
                    } else {
                        table.reload('test', {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            },
                            url: '/user/search?value=' + sessionStorage.getItem('search_value') + '&type=' + sessionStorage.getItem('search_type')
                        });
                    }
                }
            });
            return false;
        });
    });
</script>

<script type="text/html" id="barOptional">
    <!--    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>-->
    <a class="" lay-event="edit" title="编辑">
        <i class="layui-icon">&#xe642;</i>
    </a>
    &nbsp;&nbsp;&nbsp;&nbsp;
    <!--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>&nbsp;-->
    <a class="" lay-event="resetPassword" title="恢复默认密码">
        <i class="layui-icon">&#xe716;</i>
    </a>
</script>
</html>